import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:osc/utils/string_util.dart';

class SearchKeywordData{

  factory SearchKeywordData() => getInstance();

  static SearchKeywordData _instance;

  SearchKeywordData._internal(){
    _init();
  }
  static SearchKeywordData getInstance(){
    if(_instance == null){
      _instance = SearchKeywordData._internal();
    }
    return _instance;
  }


  final String key = "search_keyword";
  List<String> list;
  _init(){
    list = SpUtil.getStringList(key);
    if(list == null || list.isEmpty){
      list = List<String>();
    }
  }

  List<String> getList(){
    return list;
  }

  add({@required String keyword}){
    if(StringUtil.isEmpty(keyword)){
      return;
    }
    if(list.contains(keyword)){
      list.remove(keyword);
      list.insert(0, keyword);
    } else {
      list.insert(0, keyword);
    }
    saveList();
  }

  remove({@required String keyword}){
    if(StringUtil.isEmpty(keyword)){
      return;
    }
    if(list.contains(keyword)){
      list.remove(keyword);
    }
    saveList();
  }

  clear(){
    list.clear();
    SpUtil.remove(key);
  }

  saveList(){
    SpUtil.putStringList(key, list);
  }
}